package com.quasar.hpatient.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.utils.OutliersValues;
import com.quasar.hpatient.utils.StringUtils;
import com.rey.material.widget.Slider;
import com.vise.log.ViseLog;

/**
 * author : 许建宏
 * Date: 2017/10/26 0026
 * View:
 */

public class SeekBarEditWidget extends LinearLayout{

    String title, company, cbnight, sb_output, sb_entry;
    String min, max, val, middle, st_Type;

    //当st_Type为空的时候显示的值精确一位，不为空的时候没有小数

    TextView tv_title, tv_company, tv_min_val, tv_max_val;
    TextView et_val;
    Slider sl_slide;
    CheckBox cb_night;
    Context context;

    public SeekBarEditWidget(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public SeekBarEditWidget(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.context=context;

        //获取自定义属性
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.SeekBarEditWidget);

        title = a.getString(R.styleable.SeekBarEditWidget_sb_title);
        company = a.getString(R.styleable.SeekBarEditWidget_sb_company);
        min = a.getString(R.styleable.SeekBarEditWidget_sb_min);
        max = a.getString(R.styleable.SeekBarEditWidget_sb_max);
        val = a.getString(R.styleable.SeekBarEditWidget_sb_scale);
        middle = a.getString(R.styleable.SeekBarEditWidget_sb_middle);
        st_Type = a.getString(R.styleable.SeekBarEditWidget_sb_type);
        cbnight = a.getString(R.styleable.SeekBarEditWidget_sb_night);
        sb_output = a.getString(R.styleable.SeekBarEditWidget_sb_output);
        sb_entry = a.getString(R.styleable.SeekBarEditWidget_sb_entry);//出入量最大值
        a.recycle();

        LayoutInflater.from(context).inflate(R.layout.widght_seekbaredit_layout, this);

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_company = (TextView) findViewById(R.id.tv_company);
        et_val = (TextView) findViewById(R.id.et_editval);
        sl_slide = (Slider) findViewById(R.id.sl_slide);
        tv_min_val = (TextView) findViewById(R.id.tv_min_val);
        tv_max_val = (TextView) findViewById(R.id.tv_max_val);
        cb_night = (CheckBox) findViewById(R.id.cb_night);
        //名称
        if (title != null && title.length() > 0) {
            tv_title.setText(title);
        }
        //单位
        if (company != null && company.length() > 0) {
            tv_company.setText(company);
        }
        //检测值
        if (val != null && val.length() > 0) {
            et_val.setText(val);
        }

        if (cbnight != null) {
            cb_night.setVisibility(View.VISIBLE);
        }
        //最小值
        if (min != null && min.length() > 0 && max != null && max.length() > 0) {
            sl_slide.setValueRange(Integer.parseInt(min), Integer.parseInt(max), true);

            StringBuffer stringBufferMin = new StringBuffer(min);
            StringBuffer stringBufferMax = new StringBuffer(max);
            if (st_Type != null) {
                stringBufferMin.insert(min.length() - 1, ".").toString();
                stringBufferMax.insert(max.length() - 1, ".").toString();
            }

            if (sb_entry != null && sb_entry.length() > 0) {
                tv_max_val.setText(sb_entry);
            } else {
                tv_max_val.setText(stringBufferMax.toString());
            }
            if (title != null) {
                tv_min_val.setText(stringBufferMin.toString());

            }


        }

        //设置初始值
        if (middle != null && middle.length() > 0) {
            sl_slide.setValue(Float.parseFloat(middle), true);

            StringBuffer stringBufferMiddle = new StringBuffer(middle);
            if (st_Type != null) {
                stringBufferMiddle.insert(middle.length() - 1, ".").toString();
            }
            et_val.setText(stringBufferMiddle);
        }

        //设置步长
//        if(sb_output!=null && sb_output.length()>0){
//            sl_slide.setValue(Float.parseFloat(middle),true);
//            StringBuffer stringBufferMiddle = new StringBuffer(middle);
//            stringBufferMiddle.insert(middle.length()-1, ".").toString();
//            et_val.setText(stringBufferMiddle);
//        }


        sl_slide.setOnPositionChangeListener(new Slider.OnPositionChangeListener() {
            @Override
            public void onPositionChanged(Slider view, boolean fromUser, float oldPos, float newPos, int oldValue, int newValue) {

                change=true;
                String values = newValue + "";
                StringBuffer stringBufferMin = new StringBuffer(values);
                if (st_Type != null) {
                    stringBufferMin.insert(values.length() - 1, ".").toString();
                }

                Integer outPut = 0;
                if (sb_output != null && sb_output.length() > 0) {
                    Integer integer = Integer.parseInt(stringBufferMin.toString());
                    outPut = integer * 10;//设置步长为10
                }

                if (sb_output != null && sb_output.length() > 0) {
                    et_val.setText(outPut + "");
                } else {
                    if (values.length() == 1) {
                        et_val.setText(values);
                    } else {
                        et_val.setText(stringBufferMin);
                    }
                }
            }
        });

    }

    //检测值是否发生了改变
    boolean change=false;
    public boolean updateVal(){
        return change;
    }


    //提交后设置初始值
    public void defaultVal(String middles){
        //设置初始值
        if (middles != null && middles.length() > 0) {
            sl_slide.setValue(Float.parseFloat(middles), true);

            StringBuffer stringBufferMiddle = new StringBuffer(middles);
            if (st_Type != null) {
                stringBufferMiddle.insert(middles.length() - 1, ".").toString();
            }
            et_val.setText(stringBufferMiddle);
        }
        change=false;
    }

    public void onClickTextView(TextView.OnClickListener et_editval){
        et_val.setOnClickListener(et_editval);
    }

    //提交成功后默认为零
    public void SeekBardefault() {
        sl_slide.setValue(0, true);
    }
    //输入值
    public void EditTextView(String text){
        et_val.setText(text);
    }


    public boolean CbChoice() {
        boolean isChecked = false;
        if (cb_night.isChecked()) {
            isChecked = cb_night.isChecked();
        }

        return isChecked;
    }

    public String geTitleName() {
        return tv_title.getText().toString().trim();
    }

    public void setTitleName(String title) {
        tv_title.setText(title);
    }

    //返回检测值
    public String getDetectVal() {
        String Value = et_val.getText().toString();
        return Value;
    }

    public String getSeekBarEdit() {
        return et_val.getText().toString().trim();
    }

    public void EditCheckBox(){
        cb_night.setChecked(false);
    }
}
